<?php
/**
 * génération du sitemap du site
 * @package PWE.controler
 * @author Gmax <gmaxfr78@gmail.com>
 * 
 */
class sitemap extends controler {
    public static function getSiteMap(){
        return self::_getHeader().self::_getContents().self::_getFooter();
    }
    
    private static function _getHeader(){
        $string = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
        $string .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'."\n";
        
        return $string;
    }
    
    private static function _getContents(){
        $string = '';
        foreach(modele_category::getPrimaryListe() as $primary){
            $string .= self::_getRecursiveContent($primary,1);
        }
        foreach(modele_page::getPrimaryListe() as $primary){
            $string .= self::_getRecursiveContent($primary,1);
        }
        return $string;
    }
    /**
     * 
     * @param /modele_category $entry
     */
    private static function _getRecursiveContent($entry,$prio){
        $string = '';
        //if(!$entry->isPage()){
            $string .= "\t".'<url>'."\n";
            $string .= "\t\t".'<loc>'.$entry->getUrl().'</loc>'."\n";
            $string .= "\t\t".'<changefreq>weekly</changefreq>'."\n";
            $string .= "\t\t".'<priority>'.$prio.'</priority>'."\n";
            $string .= "\t".'</url>'."\n";
        //}
        if(!$entry->isPage() && $entry->getNombrePublications()>0){
            foreach($entry->getPublications() as $publication){
                $date = explode(' ', $publication->getDate());
                $string .= "\t".'<url>'."\n";
                $string .= "\t\t".'<loc>'.$publication->getUrl().'</loc>'."\n";
                $string .= "\t\t".'<lastmod>'.$date[0].'</lastmod>'."\n";
                $string .= "\t\t".'<changefreq>weekly</changefreq>'."\n";
                $string .= "\t\t".'<priority>'.($prio-0.1).'</priority>'."\n";
                $string .= "\t".'</url>'."\n";
            }
        }
        if($entry->haveChild()){
            foreach($entry->getChild() as $sEntry){
                $string .= self::_getRecursiveContent($sEntry, ($prio-0.2));
            }
        }
        return $string;
    }
    
    private static function _getFooter(){
        $string = '</urlset>';
        return $string;
    }
}

?>